#!/usr/bin/python
from Tkinter import *
import tkMessageBox

__all__ = ['Configuration', 'FeedList']

        
class FeedListScreen:
    def __init__(self, masterWindow, state, app):
        self.state = state
        self.app = app
        root = Toplevel(masterWindow)
        root.configure(padx=10, pady=10)
        root.title("Daily Digest: Subscriptions")
        self.create_widgets(root)
        root.mainloop()
    
    def create_widgets(self, master):
        frame = Frame(master)
        frame.pack()
        self.url = StringVar()
        
        self.lbframe = Frame(frame)
                
        self.feedlist = Listbox(self.lbframe, width=50)
        self.feedlist.grid(row=0, column=0)        

        self.scrollbar = Scrollbar(self.lbframe, command=self.feedlist.yview)
        self.scrollbar.grid(row=0, column=1, sticky=N+S)
                
        self.addfeedtext = Entry(frame, width=45, textvariable=self.url)
        self.addbutton = Button(frame, text="Add", width=4, command=self.add_feed)
        self.removebutton = Button(frame, text="Remove", width=6, command=self.remove_feed)
        
        self.lbframe.grid(row=0, column=0, columnspan=3)
        self.addfeedtext.grid(row=1, column=0)
        self.addbutton.grid(row=1, column=1)
        self.removebutton.grid(row=1, column=2)

        self.fill_list()

    def fill_list(self):
        self.feedlist.delete(0, END)
        for feed in self.state.feedManager.feedList:
            self.feedlist.insert(END, feed.feedTitle)
        
    def add_feed(self):
        self.state.feedManager.new_feed(self.url.get())
        if self.state.feedManager.errorMessage <> None:
            tkMessageBox.showerror("Feed Error", self.state.feedManager.errorMessage)
        self.fill_list()
        self.state.save_feeds()
    
    def remove_feed(self):
        print self.feedlist.curselection()
        
class ConfigurationScreen:
    def __init__(self, masterWindow, state, app):
        self.state = state
        self.app = app

        self.root = Toplevel(masterWindow)
        self.root.configure(padx=10, pady=10)
        self.root.focus_set()
        self.root.title("Daily Digest: Configuration")
                
        gui = self.create_widgets(self.root)
        self.root.mainloop()
        
    def create_widgets(self, master):
        frame = Frame(master, width=400, height=200)
        frame.pack()
                
        self.server = StringVar()
        self.username = StringVar()
        self.email_to = StringVar()
        self.stylesheet = StringVar()
        self.password = StringVar()
        self.max_posts = StringVar()
        
        self.lblUsername = Label(frame, text="smtp username:", width=15)
        self.lblSmtpServer = Label(frame, text="smtp server:", width=15)
        self.lblPassword = Label(frame, text="smtp password:", width=15)
        self.lblEmailAddr = Label(frame, text="recipient address:", width=15)
        self.lblStylesheet = Label(frame, text="stylesheet:", width=15)
        self.lblMaxPosts = Label(frame, text="max posts per feed:", width=15)

        self.entSmtpServer = Entry(frame, width=40, textvariable=self.server)
        self.entUsername = Entry(frame, width=40, textvariable=self.username)
        self.entPassword = Entry(frame, width=40, show="*", textvariable=self.password)
        self.entToAddr = Entry(frame, width=40, textvariable=self.email_to)
        self.entStylesheet = Entry(frame, width=40, textvariable=self.stylesheet)
        self.entMaxPosts = Entry(frame, width=40, textvariable=self.max_posts)


        print self.state.settings['server']
        if self.state.settings['server'] <> None:
            print "setting server.."
            self.server.set(self.state.settings['server'])
        
        if self.state.settings['username'] <> None:
            self.username.set(self.state.settings['username'])

        if self.state.settings['password'] <> None:
            self.password.set(self.state.settings['password'])
            
        if self.state.settings['stylesheet'] <> None:
            self.stylesheet.set(self.state.settings['stylesheet'])
        
        if self.state.settings['max_posts'] <> None:
            self.max_posts.set(self.state.settings['max_posts'])

        if self.state.settings['email_to'] <> None:
            self.email_to.set(self.state.settings['email_to'])
            
        self.lblSmtpServer.grid(row=0, column=0)
        self.lblUsername.grid(row=1, column=0)
        self.lblPassword.grid(row=2, column=0)
        self.lblEmailAddr.grid(row=3, column=0)
        self.lblStylesheet.grid(row=4, column=0)
        self.lblMaxPosts.grid(row=5, column=0)

        self.entSmtpServer.grid(row=0, column=1)
        self.entUsername.grid(row=1, column=1)
        self.entPassword.grid(row=2, column=1)
        self.entToAddr.grid(row=3, column=1)
        self.entStylesheet.grid(row=4, column=1)
        self.entMaxPosts.grid(row=5, column=1)
    
        self.save = Button(frame, text="save configuration", command=self.store)
        self.save.grid(row=6)
             
    def store(self):
        print "saving settings..."
        self.state.settings['email_to'] = self.email_to.get()
        self.state.settings['password'] = self.password.get()
        self.state.settings['max_posts'] = self.max_posts.get()
        self.state.settings['username'] = self.username.get()
        self.state.settings['stylesheet'] = self.stylesheet.get()
        self.state.settings['server'] = self.server.get()
        self.state.save_settings()
        self.root.destroy()
